package com.example.peibei.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.dingtao.common.bean.Dynamic;
import com.dingtao.common.bean.Order;
import com.dingtao.common.util.DateUtils;
import com.dingtao.common.util.GlideUtils;
import com.dingtao.common.util.UIUtils;
import com.example.peibei.R;
import com.example.peibei.ui.activity.OrderDetailActivity;
import com.example.peibei.ui.utils.LevelImage;

import java.text.ParseException;
import java.util.List;

public class DynamicListAdapter extends BaseQuickAdapter<Dynamic.RecordsDTO, BaseViewHolder> {
    String status;
    Boolean vip;

    public DynamicListAdapter(@Nullable List<Dynamic.RecordsDTO> data, String status) {
        super(R.layout.item_dynamic_list, data);
        this.status = status;
    }

    public DynamicListAdapter(String status) {
        super(R.layout.item_dynamic_list);
        this.status = status;
    }

    public void setVip(Boolean vip) {
        this.vip = vip;
        notifyDataSetChanged();
    }

    @SuppressLint("CheckResult")
    @Override
    protected void convert(BaseViewHolder helper, Dynamic.RecordsDTO item) {
        ImageView head = helper.getView(R.id.iv_head);
        Glide.with(getContext()).load(item.getMember().getImgUrl()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(head);
        helper.setText(R.id.tv_name, item.getMember().getNickName()).setText(R.id.tv_dec, item.getTitle()).setText(R.id.tv_adress, item.getMember().getArea());
        ImageView iv_level = helper.getView(R.id.iv_level);//会员等级
        LevelImage.setLevelImage(item.getMember().getServiceLevel(), iv_level);
        List<Dynamic.RecordsDTO.ItemDTO> items = item.getItem();
        LinearLayout llImgs = helper.getView(R.id.ll_imgs);
        ImageView imgOne = helper.getView(R.id.iv_img_one);
        ImageView imgTwo = helper.getView(R.id.iv_img_two_dynamic);
        ImageView imgThree = helper.getView(R.id.iv_img_three);
        ImageView imgFour = helper.getView(R.id.iv_img_four);
        ImageView zan = helper.getView(R.id.zan);
        TextView zan_tv = helper.getView(R.id.zan_tv);
        TextView com_tv = helper.getView(R.id.com_tv);
        TextView time = helper.getView(R.id.time);
        TextView tv_delate = helper.getView(R.id.tv_delate);
        if (status.equals("2")){
            tv_delate.setVisibility(View.VISIBLE);
        }else{
            tv_delate.setVisibility(View.GONE);
        }
        if(vip != null){
            if (vip){
                helper.setGone(R.id.iv_vip,false);
            }else {
                helper.setGone(R.id.iv_vip,true);
            }
        }else{
            if (item.isVip()){
                helper.setGone(R.id.iv_vip,false);
            }else {
                helper.setGone(R.id.iv_vip,true);
            }
        }

        if (items.size() > 0) {
            if (items.size() == 1) {
                imgFour.setVisibility(View.VISIBLE);
                llImgs.setVisibility(View.GONE);
                GlideUtils.loadImg(getContext(), items.get(0).getUrl(), 40, imgFour);
            } else {
                imgFour.setVisibility(View.GONE);
                llImgs.setVisibility(View.VISIBLE);
                if (items.get(0) != null && !items.get(0).getUrl().equals("")) {
                    GlideUtils.loadImg(getContext(), items.get(0).getUrl(), 20, imgOne);
                } else {
                    imgOne.setVisibility(View.INVISIBLE);
                }
                if (items.size() > 1 && items.get(1) != null && !items.get(1).getUrl().equals("")) {
                    GlideUtils.loadImg(getContext(), items.get(1).getUrl(), 20, imgTwo);
                } else {
                    imgTwo.setVisibility(View.INVISIBLE);
                }
                if (items.size() > 2 && items.get(2) != null && !items.get(2).getUrl().equals("")) {
                    GlideUtils.loadImg(getContext(), items.get(2).getUrl(), 20, imgThree);
                } else {
                    imgThree.setVisibility(View.INVISIBLE);
                }
            }

        } else {
            llImgs.setVisibility(View.GONE);
            imgFour.setVisibility(View.GONE);
        }
        try {
            time.setText(DateUtils.dateFormat(DateUtils.dateParse(item.getAddTime(), "yyyy-MM-dd HH:mm"), "MM-dd HH:mm"));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if (!item.isDianzan()) {
            zan.setImageResource(R.mipmap.zan_img);
        } else {
            zan.setImageResource(R.mipmap.zan_yellow);
        }
        if (item.getFollow()==0){
            zan_tv.setText("点赞");
        }else{
            zan_tv.setText(String.valueOf(item.getFollow()));
        }
        if (item.getComment() != 0) {
            com_tv.setText(String.valueOf(item.getComment()));
        } else {
            com_tv.setText("评论");
        }
    }
}
